/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Lone_Wolf
 */
public class PrintFacture extends JPanel implements Printable, ActionListener {

    //Major Components
    private Facture facture;
    private JFrame frame;
    private JPanel pnlVisible;
    //Layout components
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    //Variables
    private JLabel lblNom;
    private JLabel lblCode;
    private JLabel lblNumero;
    private JLabel lblDateFacture;
    private JLabel lblDateImpression;
    private JLabel lblCoutReel;
    private JLabel lblCoutCharge;
    //Messages
    private JLabel lblElementsCharges;
    //Fonts
    private Font f1 = new Font("Times New Roman", Font.PLAIN, 16);

    public void PrintFacture(Facture facture) {
        this.facture = facture;

        frame = new JFrame();
        pnlVisible = new JPanel();
        
        pnlVisible.add(this);
        frame.add(pnlVisible);
        frame.setVisible(true);
        frame.setSize(1, 1);
        frame.setSize(600, 50000);

        setBackground(Color.white);
        pnlVisible.setBackground(Color.white);
        frame.setBackground(Color.white);

        initComposantes();
        construireFacture();

        PrintUtilities.printComponent(pnlVisible);
        frame.removeAll();
        frame.invalidate();
        frame.repaint();
        frame.setEnabled(false);
        frame.setVisible(false);
    }

    public void initComposantes() {
        //Variables
        lblNom = new JLabel("Facturé  à : " + facture.getClient().getNom());
        lblCode = new JLabel(" [" + facture.getClient().getCode() + "]");
        lblNumero = new JLabel("Facture # " + facture.getNumero());
        lblDateFacture = new JLabel("Facture  créée  le : " + facture.getDate_S());
        lblCoutReel = new JLabel("Montant  de  la  Facture : " + facture.getCoutTotal() + "$");
        lblCoutCharge = new JLabel("Montant  Chargé : " + facture.getMontantCharge() + "$");
        
        Calendar cal = Calendar.getInstance();
        lblDateImpression = new JLabel("Imprimé  le : "
                + cal.get(cal.DATE) + "/"
                + cal.get(cal.MONTH) + "/"
                + cal.get(cal.YEAR));
        
        //Messages
        lblElementsCharges = new JLabel("Éléments Chargés : ");

        lblNom.setFont(f1);
        lblNumero.setFont(f1);
        lblCode.setFont(f1);
        lblDateFacture.setFont(f1);
        lblCoutCharge.setFont(f1);
        lblCoutReel.setFont(f1);
        lblDateImpression.setFont(f1);
    }

    public void construireFacture() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        //gbc.fill = GridBagConstraints.BOTH;

        //header
        blankSpaces(3);
        gbc.anchor = gbc.NORTHWEST;
        gbc.gridwidth = gbc.RELATIVE;
        gbl.setConstraints(lblNumero, gbc);
        this.add(lblNumero);
        gbc.gridwidth = gbc.REMAINDER;
        gbc.anchor = gbc.NORTHEAST;
        gbl.setConstraints(lblDateImpression, gbc);
        this.add(lblDateImpression);
        gbc.anchor = gbc.NORTHWEST;
        gbc.gridwidth = gbc.RELATIVE;
        gbl.setConstraints(lblNom, gbc);
        this.add(lblNom);
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblCode, gbc);
        this.add(lblCode);
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblDateFacture, gbc);
        this.add(lblDateFacture);

        //create blank space
        blankSpaces(2);
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblElementsCharges, gbc);
        this.add(lblElementsCharges);

        //on ajoute les entrées
        ajouterEntrees();
        
        //footer
        blankSpaces(2);
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblCoutReel, gbc);
        this.add(lblCoutReel);
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(lblCoutCharge, gbc);
        this.add(lblCoutCharge);
    }

    private void ajouterEntrees() {
        //let's build it
        Entree entree;
        for (int i = 0; i < facture.getNbeEntrees(); i++) {
            JButton bouton = new JButton();
            entree = facture.getEntree(i);
            bouton.setText("<html><b><i><font size=+0>" + entree.getEmploye().getNom() + "</b></i></font>"
                    + "     ---     " + entree.getTache() + " --- " + entree.getTemps()
                    + " heures pour " + entree.getCout() + "$ --- le "
                    + entree.getDay() + "-"
                    + entree.getMonth() + "-"
                    + entree.getYear() + "</html>");

            //on l'ajoute à la facture
            gbc.fill = gbc.HORIZONTAL;
            gbc.gridwidth = gbc.REMAINDER;
            gbl.setConstraints(bouton, gbc);
            this.add(bouton);
        }

    }

    private void blankSpaces(int nbe) {
        for (int i = 0; i < nbe; i++) {
            JLabel lblBlank = new JLabel(" ");
            lblBlank.setFont(f1);
            gbc.gridwidth = gbc.REMAINDER;
            gbl.setConstraints(lblBlank, gbc);
            this.add(lblBlank);
        }
    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
